<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-10
 * Time: 10:12
 * 生成邀请码，邀请助理码
 */

namespace app\api\service\invite;

use app\api\model\Member;
use app\api\model\User as UserModel;
use app\api\service\Token;
use app\lib\exception\ParameterException;
use app\lib\exception\TokenException;
use think\facade\Cache;
use think\facade\Log;

class InviteSer extends Token
{
    protected $wxAppId;
    protected $wxAppSecret;
    protected $wxCodeUrl;

    function __construct()
    {
        $this->wxAppId = config('wx.app_id');
        $this->wxAppSecret = config('wx.app_secret');
        $this->wxCodeUrl = sprintf(config('wx.code_url'),
            $this->wxAppId,$this->wxAppSecret);
    }

    /**
     * 获取用户的分享二维码
     */
    public function getInviteImg()
    {
        $user_id = self::getCurrentUid();
        $userInfo = UserModel::get($user_id);
        if($userInfo['status'] == 0) {
            throw new ParameterException([
                'errorCode' => 10001,
                'msg' => [
                    'member' => '请先进行登录',
                ]
            ]);
        }
        $member = Member::where('user_id',$user_id)->find();
        if(!$member) {
            throw new ParameterException([
                'errorCode' => 10002,
                'msg' => [
                    'member' => '请先进行会员认证',
                ]
            ]);
        }
        if($member['status'] == 1) {
            throw new ParameterException([
                'errorCode' => 10003,
                'msg' => [
                    'member' => '请等待会员信息审核通过',
                ]
            ]);
        }
        if($member['status'] == 2) {
            throw new ParameterException([
                'errorCode' => 10004,
                'msg' => [
                    'member' => '会员信息未通过',
                ]
            ]);
        }
        if($member['status'] == 3) {
            throw new ParameterException([
                'errorCode' => 10005,
                'msg' => [
                    'member' => '您已进入系统黑名单',
                ]
            ]);
        }
        $access_token = Cache::get('access_token');
        if(!$access_token)
        {
            $jsonInfo = curl_get($this->wxCodeUrl);
            $access_token = $jsonInfo["access_token"];
            $expire_in = config('setting.token_expire_in');
            $result = cache('access_token',$access_token,$expire_in);
            if(!$result)
            {
                throw new TokenException([
                    'msg' => '服务器缓存异常',
                    'errorCode' => 10005
                ]);
            }
        }
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token;
        $data['scene'] = $user_id;
        $data['width'] = 280;
        $name = 'user_'.$user_id;
        $res = https_post($url,json_encode($data));
        if(!$res) {
            $jsonInfo = curl_get($this->wxCodeUrl);
            $access_token = $jsonInfo["access_token"];
            $expire_in = config('setting.token_expire_in');
            $result = cache('access_token',$access_token,$expire_in);
            if(!$result)
            {
                throw new TokenException([
                    'msg' => '服务器缓存异常',
                    'errorCode' => 10005
                ]);
            }
            $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token;
            $data['scene'] = $user_id;
            $data['width'] = 280;
            $name = 'user_'.$user_id;
            $res = https_post($url,json_encode($data));
            return json_decode(https_post(config('setting.invite_img_url').'invite_user',[
                'res' => $res,
                'name' => $name
            ]),true);
        }  else {
            return json_decode(https_post(config('setting.invite_img_url').'invite_user',[
                'res' => $res,
                'name' => $name
            ]),true);
        }
    }


    // 获取邀请助理的二维码
    public function getAssistantImg()
    {
        self::judgeMember();
        $uid = self::getCurrentUid();
        $access_token = Cache::get('access_token');
        if(!$access_token)
        {
            $jsonInfo = curl_get($this->wxCodeUrl);
            $access_token = $jsonInfo["access_token"];
            $expire_in = config('setting.token_expire_in');
            $result = cache('access_token',$access_token,$expire_in);
            if(!$result)
            {
                throw new TokenException([
                    'msg' => '服务器缓存异常',
                    'errorCode' => 10005
                ]);
            }
        }
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token;
        $data['scene'] = $uid;
        $data['width'] = 280;
        $data['page'] = "pages/addzhuli_one/addzhuli_one";
        $name = 'zhu_'.$uid;
        $res = https_post($url,json_encode($data));
        if(!$res) {
            $jsonInfo = curl_get($this->wxCodeUrl);
            $access_token = $jsonInfo["access_token"];
            $expire_in = config('setting.token_expire_in');
            $result = cache('access_token', $access_token, $expire_in);
            if (!$result) {
                throw new TokenException([
                    'msg' => '服务器缓存异常',
                    'errorCode' => 10005
                ]);
            }
            $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
            $data['scene'] = $uid;
            $data['width'] = 280;
            $data['page'] = "pages/addzhuli_one/addzhuli_one";
            $name = 'zhu_' . $uid;
            $res = https_post($url, json_encode($data));
            $file = fopen('./qr/' . $name . '.png', "w");//打开文件准备写入
            fwrite($file, $res);//写入,$res为图片二进制内容
            fclose($file);//关闭
        } else {
            $file = fopen('./qr/'.$name.'.png',"w");//打开文件准备写入
            fwrite($file,$res);//写入,$res为图片二进制内容
            fclose($file);//关闭
        }
        return config('setting.zhuli_img_url').$name.'.png';
    }
}